import { BaseDeployPanel } from "../../../equipment/deploy/panel/BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ObjCreate } from "../../../common/ObjCreate";
import { DeployPanelItem } from "../../../equipment/deploy/DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { EBind } from "../../../../../../Message/Public/EBind";
import { MsgManager } from "../../../../manager/MsgManager";
import { Cache } from "../../../../cache/Cache";
import { ShopConfig } from "../../../../resource/ShopConfig";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { EquipmentModuleConst } from "../../../equipment/data/EquipmentModuleConst";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { EquipUpdateParams } from "../../../equipment/data/EquipUpdateParams";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { ItemData } from "../../../../resource/info/ItemData";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { ItemExInfo } from "../../../../resource/info/ItemExInfo";
import { EJewel } from "../../../../../../Message/Public/EJewel";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
type int = number;
//class PetEquipTransformPanel
    
    export  class PetEquipTransformPanel extends BaseDeployPanel
    {
        constructor()
        {
            this._useItems = [];
            this.posData = [{"x": 34, "y": 100, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 94, "y": 100, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 154, "y": 100, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 214, "y": 100, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 274, "y": 100, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 34, "y": 188, "label": Language.getString(61316), "canRemove": false, "noBoarder": true}, {"x": 94, "y": 188, "label": Language.getString(61316), "canRemove": false, "noBoarder": true}, {"x": 154, "y": 188, "label": Language.getString(61316), "canRemove": false, "noBoarder": true}, {"x": 214, "y": 188, "label": Language.getString(61316), "canRemove": false, "noBoarder": true}, {"x": 274, "y": 188, "label": Language.getString(61320), "canRemove": false, "noBoarder": true}, {"x": 154, "y": 250, "label": Language.getString(61321), "canRemove": false, "noBoarder": false}];
            this.buttonLabel = Language.getString(61345);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(61344), 0, 68, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            ObjCreate.createLoadedBitmap("arrowsDown.png", 156, 143, this);
            ObjCreate.createLoadedBitmap("Label_equip.png", 87, 155, this);
            ObjCreate.createLoadedBitmap("Label_yulang.png", 218, 155, this);
            var loc1=0;
            while (loc1 < this.posData.length) 
            {
                loc2 = this.posData[loc1];
                loc3 = new DeployPanelItem(loc1, loc2.noBoarder);
                loc3.posIndex = loc1;
                loc3.x = loc2["x"];
                loc3.y = loc2["y"];
                loc3.label = loc2["label"];
                if (loc2.canRemove) 
                {
                    loc3.mouseEnabled = true;
                    loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc3);
                allItem.push(loc3);
                ++loc1;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.onButtonClickImpl(arg1.target);
            return;
        }

        /* internal  */onButtonClickImpl(arg1): void
        {
            if (!this.checkCanRefresh()) 
            {
                return;
            }
            var loc1=arg1;
            switch (loc1) 
            {
                case confirmButton:
                {
                    if (this.checkIsChangeToBind()) 
                    {
                        Alert.show(Language.getString(60147), null, Alert.OK | Alert.CANCEL, null, this.onStartTransAlertHandler);
                    }
                    else 
                    {
                        this.onConfirm();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < 5) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.itemExInfo && allItem[loc1 + 5].itemData) 
                {
                    if (!(allItem[loc1].itemData.itemExInfo.bind == EBind._EBindYes) && allItem[loc1 + 5].itemData.itemExInfo.bind == EBind._EBindYes) 
                    {
                        return true;
                    }
                }
                ++loc1;
            }
            return false;
        }

        /* internal  */onStartTransAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */checkCanRefresh(): Boolean
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=null;
            if (this.currentEquipsNum == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60234));
                return false;
            }
            if (!allItem[10].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61331));
                return false;
            }
            if (!allItem[10].enabled) 
            {
                if (Cache.instance.equipment.isAutoBuy) 
                {
                    loc1 = ShopConfig.instance.getShopItemInfoByCode(allItem[10].itemData.itemCode);
                    var loc5=0;
                    var loc6=this._useItems;
                    for(loc4 of loc6) 
                    {
                        loc2 = loc2 + loc4.itemAmount;
                    }
                    loc3 = allItem[10].amount - loc2;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": loc1, "amount": loc3, "callBack": this.onItemBuySuccess}));
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(61325, ItemsUtil.getItemName(allItem[10].itemData)));
                }
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            if (this.currentEquipsNum > 0) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
                this.onButtonClickImpl(confirmButton);
            }
            return;
        }

        /* internal  */onConfirm(): void
        {
            var loc3=null;
            var loc5=null;
            var loc1=new EquipUpdateParams();
            loc1.operationId = this._operationId;
            var loc2=this.currentEquips;
            loc1.equipPosType = loc2[0].posType;
            var loc4=[];
            var loc6=0;
            var loc7=loc2;
            for(loc3 of loc7) 
            {
                loc4.push(loc3.uid);
            }
            loc1.equipUidArr = loc4;
            loc5 = [];
            loc6 = 0;
            loc7 = this._useItems;
            for(loc3 of loc7) 
            {
                loc5.push(loc3.uid);
            }
            loc1.materials = loc5;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc1, false, true));
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            this.showResultEquips();
            var loc2=this.currentEquipsNum;
            if (loc2 > 0) 
            {
                loc1 = EquipAdvanceConfig.instance.getPetEquipTransformCost() * loc2;
            }
            else 
            {
                loc1 = 0;
                clearAllItem();
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public putInItems(arg1: Array<any>): void
        {
            var loc1=null;
            clearAllItem();
            var loc2=0;
            while (loc2 < 5) 
            {
                if (arg1.length > loc2) 
                {
                    loc1 = arg1[loc2] as ItemData;
                    if (loc1.category == ECategory._ECategoryPetEquip) 
                    {
                        allItem[loc2].itemData = loc1;
                    }
                }
                ++loc2;
            }
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
            this.updateStatus();
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=0;
            var loc2=0;
            if (arg1.category == ECategory._ECategoryPetEquip) 
            {
                if ((loc1 = this.getIndexByUid(arg1.uid)) != -1) 
                {
                    allItem[loc1].itemData = null;
                }
                else 
                {
                    loc2 = 0;
                    while (loc2 < 5) 
                    {
                        if (!allItem[loc2].itemData) 
                        {
                            allItem[loc2].itemData = arg1;
                            break;
                        }
                        ++loc2;
                    }
                }
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
                this.updateStatus();
            }
            return;
        }

        /* internal  */getIndexByUid(arg1: string): int
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < 5) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.uid == arg1) 
                {
                    return loc1;
                }
                ++loc1;
            }
            return -1;
        }

        /* internal  */showResultEquips(): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc8=0;
            var loc9=this._useItems;
            for(loc2 of loc9) 
            {
                if (loc2.bind != EBind._EBindYes) 
                {
                    continue;
                }
                loc1 = loc1 + loc2.itemAmount;
            }
            loc3 = EquipAdvanceConfig.instance.getPetEquipTransMatsAmount();
            loc4 = Math.ceil(loc1 / loc3);
            loc7 = 0;
            while (loc7 < 5) 
            {
                if (allItem[loc7].itemData) 
                {
                    if ((loc6 = loc6 + 1) <= loc4 || allItem[loc7].itemData.itemExInfo && allItem[loc7].itemData.itemExInfo.bind == EBind._EBindYes) 
                    {
                        loc5 = EBind._EBindYes;
                    }
                    else 
                    {
                        loc5 = EBind._EBindAfterUse;
                    }
                }
                else 
                {
                    loc5 = EBind._EBindAfterUse;
                }
                this.showResultEquip(loc7, loc5);
                ++loc7;
            }
            return;
        }

        /* internal  */showResultEquip(arg1: int, arg2: int): void
        {
            var loc5=null;
            var loc6=null;
            var loc7=null;
            if (!allItem[arg1].itemData) 
            {
                allItem[arg1 + 5].itemData = null;
                return;
            }
            var loc1=allItem[arg1].itemData;
            var loc2=allItem[arg1].itemData.itemInfo.item.type;
            var loc3=loc1.itemInfo.item.effectEx != 1 ? 1 : 2;
            var loc4=ItemConfig.instance.getAllItemMap();
            var loc8=0;
            var loc9=loc4;
            for(loc6 of loc9) 
            {
                if (!(loc6.item.category == loc1.category && loc6.item.type == loc1.type && loc6.item.bind == arg2 && loc6.item.itemLevel == loc1.itemInfo.item.itemLevel && loc6.item.level == loc1.itemInfo.item.level && loc6.item.color == loc1.itemInfo.item.color && loc6.item.effectEx == loc3)) 
                {
                    continue;
                }
                loc5 = loc6;
                break;
            }
            if (loc5) 
            {
                (loc7 = new ItemData(loc5.item.code)).itemExInfo = loc1.itemExInfo.duplicate();
                loc7.itemExInfo.bind = arg2;
                this.changeAttributs(loc7.itemExInfo);
                allItem[arg1 + 5].itemData = loc7;
            }
            else 
            {
                allItem[arg1 + 5].itemData = null;
            }
            return;
        }

        /* internal  */changeAttributs(arg1: ItemExInfo): void
        {
            var loc1=0;
            var loc2=0;
            loc1 = 1;
            while (loc1 <= 6) 
            {
                loc2 = arg1["addition" + loc1];
                if (loc2 != EJewel._EJewelPower) 
                {
                    if (loc2 != EJewel._EJewelSpiritual) 
                    {
                        if (loc2 != EJewel._EJewelBrains) 
                        {
                            if (loc2 == EJewel._EJewelAgile) 
                            {
                                arg1["addition" + loc1] = EJewel._EJewelBrains;
                            }
                        }
                        else 
                        {
                            arg1["addition" + loc1] = EJewel._EJewelAgile;
                        }
                    }
                    else 
                    {
                        arg1["addition" + loc1] = EJewel._EJewelPower;
                    }
                }
                else 
                {
                    arg1["addition" + loc1] = EJewel._EJewelSpiritual;
                }
                ++loc1;
            }
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length && allItem[arg1].itemData) 
            {
                allItem[arg1].itemData = null;
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
                this.updateStatus();
            }
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            if (this.currentEquipsNum > 0) 
            {
                this._useItems = arg1.materials as Array<any>;
                allItem[10].itemData = this._useItems[0];
                allItem[10].amount = arg1.amountNeed;
                allItem[10].enabled = arg1.isEnough;
            }
            this.updateStatus();
            return;
        }

        public /* override */ clear(): void
        {
            this._useItems = [];
            clearAllItem();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            this.clear();
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61347));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(61346));
            }
            return;
        }

        /* internal  */get currentEquips(): Array<any>
        {
            var loc1=[];
            var loc2=0;
            while (loc2 < 5) 
            {
                if (allItem[loc2].itemData) 
                {
                    loc1.push(allItem[loc2].itemData);
                }
                ++loc2;
            }
            return loc1;
        }

        /* internal  */get currentEquipsNum(): int
        {
            var loc1=0;
            var loc2=0;
            while (loc2 < 5) 
            {
                if (allItem[loc2].itemData) 
                {
                    loc1 = loc1 + 1;
                }
                ++loc2;
            }
            return loc1;
        }

        /* internal */ /* var */_operationId: int=26;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;
    }
